package org.bbelovic.devel.exercises;

public class IterativeBinarySearch {

    private static final int NOT_FOUND_INDEX = -1;

    public int binarySearch(int [] arr, final int requestedItem) {
        int left = 0;
        int right = arr.length - 1;
        while (left <= right) {
            final int middle = (right + left) / 2;
            if (arr[middle] == requestedItem) return middle;
            if (requestedItem < arr[middle]) {
                right = middle - 1;
            }
            if (requestedItem > arr[middle]) {
                left = middle + 1;
            }
        }
        return NOT_FOUND_INDEX;
    }
}
